package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Module;
import com.sl.au.entity.Option;
import com.sl.au.entity.SaleArea;
import com.sl.au.service.SaleAreaService;

@RestController
@RequestMapping(value = "/ui/saleArea")
public class SaleAreaController extends BaseController {

	@Autowired
	SaleAreaService saleAreaService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		String superior = "上级部门";
		ModelAndView modelAndView = this.createLayoutView("saleArea/saleAreaindex");
		List<Option> superiors = new ArrayList<Option>();
		superiors = saleAreaService.getRepository().findByGroup(superior);
		modelAndView.addObject("superiors", superiors);
		return modelAndView;
	}

	@RequestMapping(value = "getpage", method = RequestMethod.POST)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		List<SaleArea> saleAreas = null;
		Long count = null;
		saleAreas = saleAreaService.getSaleAreas(start, length, key, null, null);
		count = saleAreaService.getRepository().getSaleAreaCount(key);
		map.put("data", saleAreas);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	// 删除片区
	@RequestMapping(value = "/deleteArea", method = RequestMethod.GET)
	public Map<String, Object> delBid(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		String id[] = ids.split(",");
		List<SaleArea> saleAreas = new ArrayList<SaleArea>();
		for (int i = 0; i < id.length; i++) {
			String areaId = id[i];
			SaleArea saleArea = new SaleArea();
			saleArea = saleAreaService.getRepository().findOne(areaId);
			if (saleArea != null) {
				saleArea.setIsAbandon(1);
			}
			saleAreas.add(saleArea);
		}
		saleAreaService.getRepository().save(saleAreas);
		map.put("code", 0);
		return map;
	}

	/// 部门下的片区是否重复
	@RequestMapping(value = "namevalidate", method = RequestMethod.GET)
	public Map<String, Object> codeValidate(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		long count =0;
		String superiorArea=request.getParameter("superiorArea");
		String saleAreaId=request.getParameter("id");
		if(saleAreaId==null||saleAreaId.equals("")){
			count=saleAreaService.getRepository().getRearea(superiorArea);
		}else{
			SaleArea saleArea=saleAreaService.getRepository().findBySuperiorArea(superiorArea);
			if(saleArea!=null){
				if(!saleArea.getId().equals(saleAreaId)){
					count=1;
				}
			}
		}
		map.put("code", count);
		return map;
	}
	@RequestMapping(value="/getall",method = RequestMethod.GET)
	public List<SaleArea> getAll() {
		List<SaleArea> list= saleAreaService.getRepository().getAllSaleArea();
		return list;
	}
	
}
